package com.iyue.portal.bean;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

/**
 * 
 * 
 * @author LUOZM
 * @version $Id: JsonResponse.java,v 1.2 2010/12/16 09:35:56 yuanming Exp $
 */
public final class JsonResponse {
	
	/**
	 * 接口日志
	 */
	protected static final Logger inteface_log = Logger
			.getLogger("inteface");
	/**
	 * 返回状态 1、成功 0、失败
	 */
	public static final String RESULT_SUCCESS = "1";
	/**
	 * 返回状态 1、成功 0、失败
	 */
	public static final String RESULT_FAILURE = "0";
	/**
	 * 错误码：系统异常
	 */
	public static final String CODE_SYSTEM_EXCEPTION = "-1";
	/**
	 * 错误码：IP鉴权失败
	 */
	public static final String CODE_IP_SAFE_FAIL = "-2";
	/**
	 * 错误码：参数错误
	 */
	public static final String CODE_PARAM_FAIL = "-3";
	
	/**
	 * 错误码
	 */
	private String errCode;
	/**
	 * 返回对象
	 */
	private String result;
	/**
	 * 返回状态
	 */
	private String status;

	public JsonResponse(String errCode, String result, String status) {
		this.errCode = errCode;
		this.result = result;
		this.status = status;
	}

	public JsonResponse() {
	}

	public boolean success() {
		if (status != null && status.equals(RESULT_SUCCESS)) {
			return true;
		}

		return false;
	}

	public boolean failure() {
		return !success();
	}

	public String getErrCode() {
		return errCode;
	}

	public void setErrCode(String errCode) {
		this.errCode = errCode;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	
	/**
	 * 接口日志
	 * 
	 * @param method
	 * @param jsonStr
	 * @param result
	 */
	public static void log(String method, String jsonStr, String result) {
		if (method == null) {
			method = "";
		}

		if (jsonStr == null) {
			jsonStr = "";
		}

		if (result == null) {
			result = "";
		}

		StringBuffer sb = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sb.append(sdf.format(new Date()));
		sb.append("|");
		sb.append(method);
		sb.append("|");
		sb.append(jsonStr);
		sb.append("|");
		sb.append(result);
		sb.append("|");

		inteface_log.info(sb.toString());
	}
}
